package com.hengda.museumonline.utils;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hengda.museumonline.MyApplication;
import com.hengda.museumonline.R;
import com.hengda.museumonline.SectionedExpandable.Item;
import com.hengda.museumonline.SectionedExpandable.ItemClickListener;
import com.hengda.museumonline.SectionedExpandable.Section;
import com.hengda.museumonline.SectionedExpandable.SectionedExpandableLayoutHelper;
import com.hengda.museumonline.entity.CategoryEvent;
import com.hengda.museumonline.entity.CategorylistBean;
import com.hengda.museumonline.entity.CityEvent;
import com.hengda.museumonline.http.CallServer;
import com.hengda.museumonline.rxbus.RxBus;
import com.hengda.museumonline.ui.MainActivity;
import com.litesuits.common.utils.PackageUtil;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import java.util.ArrayList;

/**
 * @author：JFWU on 2016/10/17 10:00
 * @E-mail：jeffy12138@126.com
 */
public class CategorylistUtil {
    private PopupWindow mPopWindow;
    private RecyclerView rv_categorylist;
    private CategorylistBean categorylistBean = null;
    private ImageView close;
    private boolean isShow = false;

    public void dismiss() {
        if (mPopWindow != null && mPopWindow.isShowing()) {
            mPopWindow.dismiss();
        }

    }

    public void getCategorylistFromServer(final MainActivity mainActivity, final ImageView categorylist, String url) {
        if (!isShow) {
            isShow = true;
            Request request = NoHttp.createStringRequest(MyApplication.url + url, RequestMethod.GET);
            request.add("p", "a");
            request.add("v", PackageUtil.getAppPackageInfo(mainActivity).versionName);

            CallServer.getRequestInstance().add(mainActivity, 0, request, new OnResponseListener() {
                @Override
                public void onStart(int what) {
                }

                @Override
                public void onSucceed(int what, Response response) {
                    Gson gson = new Gson();
                    categorylistBean = gson.fromJson(response.get().toString(), CategorylistBean.class);
                    View contentView = LayoutInflater.from(mainActivity).inflate(R.layout.popuplayout, null);
                    close = (ImageView) contentView.findViewById(R.id.close);
                    close.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mPopWindow.dismiss();
                            isShow = false;
                        }
                    });
                    rv_categorylist = (RecyclerView) contentView.findViewById(R.id.rv_categorylist);
                    SectionedExpandableLayoutHelper sectionedExpandableLayoutHelper = new SectionedExpandableLayoutHelper(mainActivity,
                            rv_categorylist, new ItemClickListener() {
                        @Override
                        public void itemClicked(Item item) {
                            Toast.makeText(mainActivity, "Item: " + item.getName() + " clicked", Toast.LENGTH_SHORT).show();
                            RxBus.get().post("category", new CategoryEvent(item.getName(), item.getId()));
                            mPopWindow.dismiss();
                            isShow = false;
                        }

                        @Override
                        public void itemClicked(Section section) {

                        }
                    }, 3);
                    for (int i = 0; i < categorylistBean.getData().getList().size(); i++) {
                        ArrayList<Item> arrayList = new ArrayList<>();
                        for (int j = 0; j < categorylistBean.getData().getList().get(i).getList().size(); j++) {
                            arrayList.add(new Item(categorylistBean.getData().getList().get(i).getList().get(j).getCname(), j));
                        }
                        sectionedExpandableLayoutHelper.addSection(categorylistBean.getData().getList().get(i).getName(), arrayList);
                    }
                    sectionedExpandableLayoutHelper.notifyDataSetChanged();
                    mPopWindow = new PopupWindow(contentView);
                    mPopWindow.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
                    mPopWindow.setHeight(ViewGroup.LayoutParams.FILL_PARENT);
                    mPopWindow.showAtLocation(categorylist, Gravity.BOTTOM, 0, 0);
                }

                @Override
                public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
                    System.out.println("onFailed:" + exception);
                }

                @Override
                public void onFinish(int what) {
                }
            });
        }

    }
}
